package com.leyou.page.service;


import com.leyou.item.pojo.*;
import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecClient;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Log4j2
@Service
public class GoodsService {

    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private SpecClient specClient;


    //封装数据,填充静态页面,根据spu的id获取spu,spu详情页;sku集合;商品分类,品牌,规格组,sku特有规格参数:id:name
    public Map<String, Object> loadModel(Long id) {
        try {
            Map<String, Object> map = new HashMap<>();
            //获取spu信息
            Spu spu = this.goodsClient.querySpuById(id);
            map.put("spu",spu);

            //获取分类的名称.分别保存在map中;id:name
            List<Category> categorys= getCategory(spu);
            if (categorys!=null){
                map.put("categorys",categorys);
            }


            //获取品牌的信息
            List<Brand> brands = this.brandClient.queryBrandByIds(Arrays.asList(spu.getBrandId()));
            map.put("brand",brands.get(0));

            //获取sku集合,
            List<Sku> skus = this.goodsClient.querySkuBySpuId(spu.getId());
            map.put("skus",skus);

            //获取详情页信息
            SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spu.getId());
            map.put("spuDetail",spuDetail);

            //获取规格组信息
            List<SpecGroup> groups = this.specClient.querySpecGroups(spu.getCid3());
            map.put("groups",groups);

            //获取sku的特有规格参数
            List<SpecParam> specParams = this.specClient.querySpecParam(null,spu.getCid3(),null,null);
            Map<Long,String> paramMap=new HashMap<>();
            for (SpecParam specParam : specParams) {
                paramMap.put(specParam.getId(),specParam.getName());//4:颜色
            }
            map.put("paramMap",paramMap);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("spu信息封装失败"+e.getMessage());
            return null;
        }
    }
   //获取分类的信息id:name:三级联动
    private List<Category> getCategory(Spu spu) {
        try {
            List<String> names = this.categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            List<Category> categorys = new ArrayList<>();
            Category category1 = new Category();
            category1.setId(spu.getCid1());
            category1.setName(names.get(0));
            categorys.add(category1);

            Category category2 = new Category();
            category2.setId(spu.getCid2());
            category2.setName(names.get(1));
            categorys.add(category2);

            Category category3 = new Category();
            category3.setId(spu.getCid3());
            category3.setName(names.get(2));
            categorys.add(category3);

            return categorys;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取分类的信息失败"+e.getMessage());
            return null;
        }
    }
}
